package com.xc.utils;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.xc.entity.query.PageResult;

import java.util.List;

public class ConvertEntity {

    public static class Builder<T, M> {
        private T dateSrc;
        private Class<M> outputCla;

        public Builder<T, M> from(T dateSrc) {
            this.dateSrc = dateSrc;
            return this;
        }

        public Builder<T, M> to(Class cla) {
            this.outputCla = cla;
            return this;
        }

        public PageResult build() {
            if (dateSrc == null) {
                return new PageResult();
            }
            List<M> list = BeanUtil.copyToList(((IPage) dateSrc).getRecords(), outputCla);
            PageResult<List<M>> result = new PageResult<>();
            result.setRecordsSum(((IPage) dateSrc).getTotal());
            result.setPageNo(((IPage) dateSrc).getCurrent());
            result.setPageSize(((IPage) dateSrc).getSize());
            result.setList(list);
            return result;
        }
    }
}
